package com.lushu.chapitre1.graphics.controls {
    import com.sfdk.core.KeyboardListener;
    import com.sfdk.graphics.api.IDisplayObjectContainerEx;
    import com.sfdk.graphics.core.TextFieldEx;
    import com.sfdk.graphics.ui.parts.ComponentBase;
    import com.sfdk.locale.LocalisedMessageEngine;
    import com.sfdk.pools.BitmapDataPool;
    import com.sfdk.pools.MusicPool;
    import com.sfdk.pools.WaveSoundPool;

    import flash.display.Bitmap;

    import flash.events.Event;
    import flash.events.KeyboardEvent;

    public class RoamingMenu extends ComponentBase
    {
        private var _menuContainer : Bitmap;

        private var _menuInfoBackground : Bitmap;
        private var _menuInfoTextField : TextFieldEx;

        private var _magicButton : Bitmap;
        private var _magicButton2 : Bitmap;

        private var _memberButton : Bitmap;
        private var _memberButton2 : Bitmap;

        private var _itemButton : Bitmap;
        private var _itemButton2 : Bitmap;

        private var _searchButton : Bitmap;
        private var _searchButton2 : Bitmap;

        private var _initialBtnName : String;
        private var _scale : Number;
        private var _currentBtn : Bitmap;

        private var _currentFrame : int;
        private var _targetYPosition : int;

        public static const MAGIC_BTN : String = "MAGIC_BTN";
        public static const SEARCH_BTN : String = "SEARCH_BTN";
        public static const MEMBER_BTN : String = "MEMBER_BTN";
        public static const ITEM_BTN : String = "ITEM_BTN";

        private var _textBox:TextBox;
        private var _hideAfterwards:Boolean;



        public function RoamingMenu(initialBtnName : String, scale : Number)
		{
            _initialBtnName = initialBtnName;
            _scale = scale;
            super();
		}

        override protected function createChildren():void
        {
            super.createChildren();
            _menuContainer = BitmapDataPool.instance.getAsBitmap("resources/images/menuContainer.png");

            _magicButton = BitmapDataPool.instance.getAsBitmap("resources/images/magic.png");
            _magicButton.scaleX = _scale;
            _magicButton.scaleY = _scale;

            _magicButton2 = BitmapDataPool.instance.getAsBitmap("resources/images/magic2.png");
            _magicButton2.scaleX = _scale;
            _magicButton2.scaleY = _scale;
            _magicButton2.visible = false;

            _memberButton = BitmapDataPool.instance.getAsBitmap("resources/images/member.png");
            _memberButton.scaleX = _scale;
            _memberButton.scaleY = _scale;

            _memberButton2 = BitmapDataPool.instance.getAsBitmap("resources/images/member2.png");
            _memberButton2.scaleX = _scale;
            _memberButton2.scaleY = _scale;
            _memberButton2.visible = false;

            _itemButton = BitmapDataPool.instance.getAsBitmap("resources/images/item.png");
            _itemButton.scaleX = _scale;
            _itemButton.scaleY = _scale;

            _itemButton2 = BitmapDataPool.instance.getAsBitmap("resources/images/item2.png");
            _itemButton2.scaleX = _scale;
            _itemButton2.scaleY = _scale;
            _itemButton2.visible = false;

            _searchButton = BitmapDataPool.instance.getAsBitmap("resources/images/search.png");
            _searchButton.scaleX = _scale;
            _searchButton.scaleY = _scale;

            _searchButton2 = BitmapDataPool.instance.getAsBitmap("resources/images/search2.png");
            _searchButton2.scaleX = _scale;
            _searchButton2.scaleY = _scale;
            _searchButton2.visible = false;

            _menuInfoBackground = BitmapDataPool.instance.getAsBitmap("resources/images/menuInfo.png");
            _menuInfoTextField = new TextFieldEx("SFSys-12");

            setTargetButton(_initialBtnName);
        }

        override protected function mapHotKeys():void
        {
            super.mapHotKeys();
        }

        override protected function cleanView(e : Event) : void
        {
            super.cleanView(e);
        }

        override protected function createView(e : Event) : void
		{
			super.createView(e);

            attachChild(_menuContainer, 0, 0, "menuContainer");

            attachChild(_magicButton, 0, 24, "magicButton");
            attachChild(_magicButton2, 0, 24, "magicButton2");

            attachChild(_memberButton, 2 * 24, 0, "memberButton");
            attachChild(_memberButton2, 2 * 24, 0, "memberButton2");

            attachChild(_itemButton, 4 * 24, 24, "itemButton");
            attachChild(_itemButton2, 4 * 24, 24, "itemButton2");

            attachChild(_searchButton, 2 * 24, 48, "searchButton");
            attachChild(_searchButton2, 2 * 24, 48, "searchButton2");

            attachChild(_menuInfoBackground, 160, 48, "menuInfo");
            attachChild(_menuInfoTextField, 160 + 16, 48 + 14);
		}

        override public function update():void {
            super.update();
            if(this.y != _targetYPosition)
            {
                if(this.y < _targetYPosition)
                    this.y += 30;
                else
                    this.y -= 30;
            }
            else
            {
                if(_hideAfterwards)
                    this.visible = false;
            }
            if(_currentBtn && _currentFrame <= 0)
            {
                _currentBtn.visible = !_currentBtn.visible;
                _currentFrame = 14;
            }
            else
            {
                _currentFrame--;
            }
            if(_textBox)
                _textBox.update();
        }

        private function resetAnimation() : void
        {
            _magicButton2.visible = false;
            _memberButton2.visible = false;
            _itemButton2.visible = false;
            _searchButton2.visible = false;
            _currentFrame = 14;
        }

        public function setTargetY(yPos : int, hideAfterwards : Boolean = false) : void
        {
            this.visible = true;
            MusicPool.getFromPool("resources/sounds/Menu Pop.mp3").play();
            _targetYPosition = yPos;
            _hideAfterwards = hideAfterwards;
        }

        public function setTargetButton(buttonName : String) : void
        {
            resetAnimation();
            switch(buttonName)
            {
                case MAGIC_BTN:
                    _currentBtn = _magicButton2;
                    _menuInfoTextField.text = LocalisedMessageEngine.getMessage(MAGIC_BTN);
                break;

                case SEARCH_BTN:
                    _currentBtn = _searchButton2;
                    _menuInfoTextField.text = LocalisedMessageEngine.getMessage(SEARCH_BTN);
                break;

                case MEMBER_BTN:
                    _currentBtn = _memberButton2;
                    _menuInfoTextField.text = LocalisedMessageEngine.getMessage(MEMBER_BTN);
                break;

                case ITEM_BTN:
                    _currentBtn = _itemButton2;
                    _menuInfoTextField.text = LocalisedMessageEngine.getMessage(ITEM_BTN);
                break;

            }
        }
        /**
         *  UI Handlers
         */

        public function handleArrowKeys(e : Event) : void
        {
            var targetBtn : Bitmap;
            var targetText : String;
            switch(e.type)
            {
                case "LEFT":
                    targetBtn = _magicButton2;
                    targetText = LocalisedMessageEngine.getMessage(MAGIC_BTN);
                break;

                case "RIGHT":
                    targetBtn = _itemButton2;
                    targetText = LocalisedMessageEngine.getMessage(ITEM_BTN);
                break;

                case "UP":
                    targetBtn = _memberButton2;
                    targetText = LocalisedMessageEngine.getMessage(MEMBER_BTN);
                break;

                case "DOWN":
                    targetBtn = _searchButton2;
                    targetText = LocalisedMessageEngine.getMessage(SEARCH_BTN);
                break;
            }
            if(_currentBtn != targetBtn)
            {
                MusicPool.getFromPool("resources/sounds/Menu Move.mp3").play();
                resetAnimation();
                targetBtn.visible = true;
                _currentBtn = targetBtn;
                _menuInfoTextField.text = targetText
            }
        }

        public function handleEnterKey(e : Event) : void
        {
            MusicPool.getFromPool("resources/sounds/Menu Confirm.mp3").play();
            switch(_currentBtn)
            {
                case _searchButton2:
                    dispatchEvent(new Event("SEARCH_ACTION", true));
                break;
            }
        }
    }
}
